Create interactive Candlestick charts of OHLCV data obtained from yfinance library using python libraries.
Published
January 26, 2024
Introduction
In this post, we will be looking at creating candlestick charts, which are integral to traders’ daily routines, using Python libraries such as Mplfinance, Plotly and Bokeh, and look at customizing the plots, change themes and tweak some colors.
As this blog post is written on Quarto, it enables viewers to interact with the output plots. You’ll be able to pan and zoom with the charts created with plotly and bokeh!
So, now lets head on to fetch our data first.
Getting Data
We will make use of the yfinance library. Let’s fetch the Open-High-Low-Close and Volume data for Apple (AAPL) for the past 5 months.
Our data looks like this:
Code
import yfinance as yfimport pandas as pdimport warningswarnings.simplefilter(action='ignore', category=FutureWarning)data = yf.download("AAPL", period="5mo",auto_adjust=True)data.head()
C:\Users\soumy\AppData\Roaming\Python\Python310\site-packages\yfinance\base.py:48: FutureWarning:
The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
[*********************100%%**********************] 1 of 1 completed
Open
High
Low
Close
Volume
Date
2023-08-28
179.853052
180.352394
178.315085
179.952927
43820700
2023-08-29
179.463564
184.656719
179.263830
183.877747
53003900
2023-08-30
184.696681
187.602856
184.496947
187.403107
60813900
2023-08-31
187.592859
188.871173
187.233332
187.622818
60794500
2023-09-01
189.240698
189.670125
188.032284
189.210739
45732600
Plots
Now we will head straight into plotting. We will start with mplfinance, a special module for finance charts of matplotlib.
mplfinance
Creating candlestick charts with mplfinance is a simple one line code:
Code
import mplfinance as mpfmpf.plot(data, type="candle", volume=True, style="yahoo", figsize=(6,3),title="AAPL Candlestick with Mplfinance", ylabel="Price [USD]")
This plot uses the “yahoo” style. We can check the available style using the command mpf.available_styles(). Now let us look at a few other available styles below:
And now we will tweak and play with the candle colors and background. First we will set the base mplstyle to dark_background (for reference, check here). Then we will set marketcolors for the chart:
Code
mc = mpf.make_marketcolors(up="g",down="r",inherit=True)s = mpf.make_mpf_style(base_mpl_style="dark_background",marketcolors=mc)mpf.plot(data, type="candle", volume=True, style=s, figsize=(6,3),title="Customised Dark theme plot on mplfinance", ylabel="Price [USD]")
For looking further stye changes using mplfinance, one might check their example notebook
We will move ahead and try hands on some interactive plotting libraries.
Plotly
Plotly is perhaps the most used interactive visualization library in python. It offers a great deal of customization. With Plotly, we will make two types of charts, OHLC and Volume plotted together in first one, separate in the next.
Plotly as well offers multiple themes and customizations, for now will use the dark theme and just change the colors of volume bars for the first plot.
Bokeh is an interactive visualization library for modern web browsers. It provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large or streaming datasets. Bokeh can help anyone who would like to quickly and easily make interactive plots, dashboards, and data applications.
Bokeh additionally has great documentation and user guide. Now let’s look at a candlestick chart created using Bokeh:
That really looks good. If you are going through the code here, you can see how simple it would be to change the colors of bars. One can further add tooltips that will display the OHLC data on hovering over the candles.
One might further look at libraries like Altair, ggplot. One great library that I just found while writing this post is Holoviews